

clear all

set more off 


***************************************



*is it onset (=1) or incidence(=.)?
local cheat 1

if "`cheat'" == "." {
	local on_or_in incidence
}
if "`cheat'" == "1" {
	local on_or_in onset
}

***************************************
***************************************


global models 
forval v = 1 / 1 {
	global models $models `v' `v'w
}


program prepare_roc

	
	local which_thetas ste
	*foreach x in one two {
	foreach x in one {
	
		if "`x'" == "one" {
			local before 1
		}
		if "`x'" == "two" {
			local before 2
		}
																
		*****ll=0 is all countries, 1 only countries that ever had conflict
		forvalues ll = 0/0 {
												

			local model 0
			foreach vv in $models {
				
				clear
				use tempx
					
				keep if conflict != `2'
				drop if p_`x'_model`vv' == .
				drop if  `x'_before  == .
					
				keep  p_`x'_model`vv' `x'_before total_conflict conflict
				
				local model = `model' + 1 


				sort p_`x'_model`vv'									
					
				egen rank = rank(p_`x'_model`vv') if  p_`x'_model`vv' != .
							
					
				sum rank
					
				local max_rank `r(max)'
					
					
				if `r(N)' > 0 {
																
					
					forval cut = 1 / `max_rank' {
					
						local tpn 0
						local fpn 0
						local tpd 0
						local fpd 0
						
			
						*true positive
						quietly gen tp = 1 if  rank>= `cut' & rank!= . &  `x'_before == 1 &  total_conflict >= `1' & conflict!=`2' 
						*false positive						
						quietly gen fp = 1 if   rank>= `cut' & rank!= . &  `x'_before == 0 &  total_conflict >= `1' & conflict!=`2' 
						*wars
						quietly sum `x'_before if    rank!= . & total_conflict >= `1' & conflict!=`2'
						if `r(N)'!= 0 {
							*total `x'_before within evaluation sample
							local tpd = `r(N)' * `r(mean)'
							local fpd = `r(N)' * (1 - `r(mean)')
						}
			
						*warning bell rang when war
						quietly cap sum tp if   rank!= . &  total_conflict >= `1' & conflict!=`2'
						if `r(N)'!= 0 {
							local tpn = `r(N)' * `r(mean)'							
						}
					
						*warning bell rang when no war
						quietly sum fp if   rank!= . &  total_conflict >= `1' & conflict!=`2'
						if `r(N)'!= 0 {
							local fpn = `r(N)' * `r(mean)'							
						}
		
					
							
							
						drop tp fp
						quietly display "`vv'"
						post buffer (`cut') (`model') (`before') (`2')  (`tpn') (`fpn') (`tpd') (`fpd')
						*file write buffer (`cut') ("`vv'") ("`x'") (`2')  (`tpn') (`fpn') (`tpd') (`fpd')
				
							
					}
					
				}
					
				drop rank
				
									
				quietly display "`vv'"
				
				
			}
			
		}
	}

end



local runs = 0

foreach k in  0 1 3 5 10 {
	foreach l in 001 005 {
		foreach j in 5 15 50 70  {
		
			local C 3
			local C0 2
			if `j' == 15 {
				local C 8
				local C0 1
			}
			forval c = `C0' / `C'  {

				*local runs = `runs' + 1
				display "output/robust_predict_one_`on_or_in'_int`c'_to`j'_alpha`k'_beta`l'_all_both"
				capture confirm file "output/robust_predict_one_`on_or_in'_int`c'_to`j'_alpha`k'_beta`l'_all_both.dta"
				*capture confirm file robust_predict_one_onset_int6_to15_alpha3_beta001_all_both.dta
				display _rc
				if _rc==0  {
	
					use "output/robust_predict_one_`on_or_in'_int`c'_to`j'_alpha`k'_beta`l'_all_both", clear
					
					drop conflict
					merge 1:1 countryid year using "complete_main_new.dta", keepusing(bdbest25 bdbest1000 violence bdbest34 refugees pop conflict_int)
	
	
					generate conflictpc = bdbest34/pop>0.08
					replace conflictpc = . if bdbest34==. | pop==.
	

								
						
						
								
							
								if `c' == 2 {
						
									quietly gen conflict = bdbest25
									
								}
						
								if `c' == 3 {
															
									quietly gen conflict = bdbest1000
							
								}
								
								if `j' == 15 {
								
									if `c' == 1 {
						
										quietly gen conflict = violence
											
									}
								
								
									if `c' == 4 {
															
										quietly gen conflict = conflictpc
							
									}
								
									if `c' == 5 {
															
										quietly gen conflict = 0 if refugees != .
										quietly replace conflict = 1 if refugees != . & refugees > 30000
							
									}
								
									if `c' == 6 {
																
										quietly gen conflict = 0 if refugees != .
										quietly replace conflict = 1 if refugees != . & refugees > 130000
							
									}
								
									if `c' == 7 {
															
										quietly gen conflict = max(conflict_int, bdbest25)
							
									}
								
									if `c' == 8 {
															
										quietly gen conflict = max(conflict_int, bdbest1000)
							
									}
									
								}

				

					sort countryid year
	
					gen total_conflict = 0	
	
					tsset countryid year
					generate one_before=conflict==0&F1.conflict==1
					generate two_before=conflict==0&(F1.conflict==1|F2.conflict==1)
				
					if "`cheat'" == "." {
						replace one_before= 1 if conflict==1&F1.conflict==1
						replace two_before= 1 if conflict==1&(F1.conflict==1|F2.conflict==1)
					}
	
	
					drop if year < 1975	
	
	

					save tempx, replace
	
					postfile buffer cut model before conftype tpn fpn tpd fpd using output/for_roc_int`c'_to`j'_alpha`k'_beta`l'_`on_or_in', replace
					prepare_roc 0 `cheat'
					postclose buffer
					
					clear
					use output/for_roc_int`c'_to`j'_alpha`k'_beta`l'_`on_or_in'
					
					gen tpr = tpn / tpd
					gen fpr = fpn / fpd	
	

					*foreach j in $models {
					forval y = 0 / 1 {
						local new = _N + 1
				    	set obs `new'
				    	replace tpr = `y' if _n == _N
			   	 		replace fpr = `y' if _n == _N
			   	 		replace model = 1 if _n == _N
   	 				}
			   	 	forval y = 0 / 1 {
						local new = _N + 1
				    	set obs `new'
	    				replace tpr = `y' if _n == _N
			   	 		replace fpr = `y' if _n == _N
			   	 		replace model = 2 if _n == _N
   	 				}

	
					save output/blub, replace
					collapse (max) tpr, by(fpr model)
					save output/blub1, replace
					use output/blub
					collapse (min) tpr, by(fpr model)
					append using output/blub1
					save output/blub, replace
					collapse (max) fpr, by(tpr model)
					save output/blub1, replace
					use output/blub
					collapse (min) fpr, by(tpr model)
					append using output/blub1
	


					sort model fpr tpr
	
					duplicates drop
	
					   
	
					*compute AUC
	
	
					sort model fpr tpr
					gen fpr_dif = fpr - fpr[_n-1] if model == model[_n-1]
					gen tpr_dif = tpr - tpr[_n-1] if model == model[_n-1]
					*gen area = tpr * fpr_dif
					gen area = 0.5 * tpr_dif * fpr_dif + tpr[_n-1] * fpr_dif if model == model[_n-1]
					by model: egen auc = sum(area)
	
	
					save output/for_roc_int`c'_to`j'_alpha`k'_beta`l'_`on_or_in', replace
				}
			}
		}
	}
	
}






erase tempx.dta




*local on_or_in onset
*local on_or_in incidence

***plotting

local left_solid_name Overall Model
local left_dash_name Within Model

local right_solid_name Overall Model
local right_dash_name Within Model



foreach k in 0 1 3 5 10 {
	foreach l in 001 005 {
		foreach j in 5 15 50 70  {

			local fname figE
			local C 1
			
			if `j' == 15 {
			
				local C 4
				*local fname figE
				
			}

				forval c = 1 / `C' {
				
				
				if `c' == 1 {
					local c1 3
					local c2 2
					
					local title_left Civil War
					local title_right Armed Conflict

					if `j' == 5 {
						local fname figE2
					}
					if `j' == 50 {
						local fname figE3
					}
					if `j' == 70 {
						local fname figE4
					}
					

				} 
				
				if `c' == 2 {
					local c1 1
					local c2 4
					
					local title_left Repression
					local title_right Per Capita Conflict
					local fname figE9
					
				}
				
				if `c' == 3 {
					local c1 5
					local c2 6
					local title_left Refugees (low)
					local title_right Refugees (high)
					local fname figE11
				}
				
				if `c' == 4 {
					local c1 7
					local c2 8
					local title_left Civil War or International Conflict
					local title_right Armed or International Conflict
					local fname figE8
				}
	
				
				capture confirm file "output/for_roc_int`c1'_to`j'_alpha`k'_beta`l'_`on_or_in'.dta"
				display "output/for_roc_int`c1'_to`j'_alpha`k'_beta`l'_`on_or_in'.dta"
				display _rc
				if _rc==0  {
	
					use "output/for_roc_int`c1'_to`j'_alpha`k'_beta`l'_`on_or_in'", clear
					
			sort model fpr tpr
			*graph of war
						graph drop _all

			sum auc if model == 1
			if `r(N)' > 0 {
				local auc1 = substr(string(round(`r(max)',.01)), 1, 3)
				sum auc if model == 2
				local auc2 = substr(string(round(`r(max)',.01)), 1, 3)

				twoway (line tpr fpr if model == 1, lwidth(medthick) lpattern(solid) connect(l) sort) ///
				(line tpr fpr if model == 2, lwidth(medthick) lpattern(dash) connect(l) sort) ///
				 (line fpr fpr if model == 1, color(gs10) lpattern(dash) sort), ///
				subtitle("a) `title_left'", color(black)) ytitle(`"True Positive Rate"') xtitle(`"False Positive Rate"') legend( order(1 "`left_solid_name'" 2 "`left_dash_name'") size(small)) xlabel(,grid) ///
				plotregion(fcolor(white)) graphregion(fcolor(white)) name(fig1) aspectratio(1) ///	
				caption( "AUC: `left_solid_name' `auc1', `left_dash_name' `auc2'", size(small) color(black) ring(0)  placement(se))


				use "output/for_roc_int`c2'_to`j'_alpha`k'_beta`l'_`on_or_in'", clear
			
				sum auc if model == 1
				local auc1 = substr(string(round(`r(max)',.01)), 1, 3)
				sum auc if model == 2
				local auc2 = substr(string(round(`r(max)',.01)), 1, 3)

				twoway (line tpr fpr if model == 1, lwidth(medthick) lpattern(solid) connect(l) sort) ///
				(line tpr fpr if model == 2, lwidth(medthick) lpattern(dash) connect(l) sort) ///	
				 (line fpr fpr if model == 1, color(gs10) lpattern(dash) sort), ///
				subtitle("b) `title_right'", color(black)) ytitle(`"True Positive Rate"') xtitle(`"False Positive Rate"') legend( order(1 "`right_solid_name'" 2 "`right_dash_name'") size(small)) xlabel(,grid) ///
				plotregion(fcolor(white)) graphregion(fcolor(white)) name(fig2) aspectratio(1) ///
				caption( "AUC: `right_solid_name' `auc1', `right_dash_name' `auc2'", size(small) color(black) ring(0)  placement(se))


				grc1leg fig1 fig2 , legendfrom(fig1) title("", color(black)  size(small)) subtitle("", color(black) size(small)) plotregion(fcolor(white)) graphregion(fcolor(white)) 

				graph export output/`fname'.eps ,replace

				
			}
			
		}
		
		}


		}
	}
}









*erase for_roc1.dta
*erase for_roc2.dta

